<?

//Data loading
$title='TRIAL BALANCE';
if (GetParam("office","")) $perush = GetCompany (GetParam("office",""));
else $perush = 'PT. MASAJI PRAYASA CARGO';

$page   = 'PAGE';

$DEFINE_FONT=0;
$pdf=new PDFReport('P',"mm","Letter");
$pdf->AddPage();
$Result = GridList ();
if ($Result) $pdf->GridTable($Result);
$pdf->Output();

function GetFromDate () {
    list($Day,$Month,$Year,$NMonth) = explode("-",date("d-m-Y-F",mktime(0,0,0,GetParam("m",""),1,GetParam("y",""))));
    return array($Day,$Month,$Year,$NMonth);
}

function GetLastPeriod ($Month, $Year) {
    list($LDay,$LMonth,$LYear,$LNMonth) = explode("-",date("d-m-Y-F",mktime(0,0,0,$Month,0,$Year)));
    return array($LMonth,$LYear,$LNMonth);
}


function GetLastBl() {
            global $DBConnection;
            list($Day,$Month,$Year,$NMonth) = GetFromDate();
            list($LMonth,$LYear,$LNMonth) = GetLastPeriod ($Month, $Year);

            //Balance Last Month

            if ($LMonth==12) $modcondition = " awal";
            else $modcondition = " aku$LMonth";
                        $SQL = "SELECT sum($modcondition) FROM tblcoa WHERE ledger='$Year' AND skada<>'".Get_salrlytd()."'";
                        if (GetParam("office","")) $SQL .= " AND ktrasl='".GetParam("office","")."'";
                        return $DBConnection->dbc->get_var($SQL);
}

function GridList () {
            global $DBConnection;

            list($Day,$Month,$Year,$NMonth) = GetFromDate();
            list($LMonth,$LYear,$LNMonth) = GetLastPeriod ($Month, $Year);

            //Balance Last Month
                        $TEMP = "TEMPORARY";
            if ($LMonth==12) $modcondition = "awal";
            else $modcondition = "aku$LMonth";

                        $SQL = "DROP TABLE IF EXISTS `ais_tmpmemo`";
                        $DBConnection->dbc->query($SQL);

            $SQL =  "CREATE $TEMP TABLE `ais_tmpmemo` (".
                    "`skada` VARCHAR( 64 ) NOT NULL ,".
                    "`tglbkt` DATE NOT NULL ,".
                    "`debet` DECIMAL( 20, 2 ) NOT NULL ,".
                    "`credit` DECIMAL( 20, 2 ) NOT NULL, ".
                                        "`ktrasl` VARCHAR( 4 ) NOT NULL )";
            $DBConnection->dbc->query($SQL);
            //print $SQL;die();

            $SQL = "INSERT INTO `ais_tmpmemo` (skada, tglbkt, debet, credit, ktrasl) SELECT skada, tglbkt, sum(IF(dk='D',rupiah,0)) as debet, sum(IF(dk='K',rupiah,0)) as credit, ktrasl
                                    FROM ais_memorial WHERE (nobkt not like 'MC%' OR nobkt not like 'ME%') AND MONTH(tglbkt)=".($Month*1)." AND YEAR(tglbkt)=$Year ";
                        if (GetParam("office","")) $SQL .= " AND ktrasl='".GetParam("office","")."'";
                        $SQL .= " GROUP BY ais_memorial.skada ";
            $DBConnection->dbc->query($SQL);
            //print $SQL;die();

                        $SQL = "DROP TABLE IF EXISTS `tblcoa_tmp`";
                        $DBConnection->dbc->query($SQL);

            $SQL =  "CREATE $TEMP TABLE `tblcoa_tmp` (".
                    "`skada` VARCHAR( 64 ) NOT NULL ,".
                    "`namacc` VARCHAR( 64 ) NOT NULL,".
                                        "`$modcondition` DECIMAL( 15,2 ) NOT NULL
                                        )";
            $DBConnection->dbc->query($SQL);


                        $SQL  = "INSERT INTO tblcoa_tmp SELECT skada, namacc, sum($modcondition) FROM tblcoa WHERE 1 AND tipe=0 AND ledger='$Year'";
                        if (GetParam("office","")) $SQL .= " AND ktrasl='".GetParam("office","")."'";


                        $SQL .= " GROUP BY skada";
                        $DBConnection->dbc->query($SQL);

            $SQL  = "SELECT tblcoa_tmp.skada, tblcoa_tmp.namacc, ais_tmpmemo.debet as debet, ais_tmpmemo.credit as credit, tblcoa_tmp.$modcondition as lastbl FROM tblcoa_tmp
                                    LEFT JOIN ais_tmpmemo ON ais_tmpmemo.skada=tblcoa_tmp.skada
                                    WHERE 1 AND tblcoa_tmp.skada<>'".Get_salrlytd()."' ";

                        if (GetParam("office","")) $SQL .= " AND ais_tmpmemo.ktrasl='".GetParam("office","")."'";
                        $SQL .= " ORDER BY tblcoa_tmp.skada ";
                        //print $SQL;die();
            $MyResult = $DBConnection->dbc->get_results($SQL,ARRAY_A);
            return $MyResult;
}

function Get_salrlytd() {
   global $DBConnection;
  $SQL = "SELECT salrlytd FROM tblprofile";
  return $DBConnection->dbc->get_var($SQL);
}

function BuildPeriod ($year, $month) {
  $arraymonth = array("","JANUARI","FEBRUARI","MARET","APRIL","MEI","JUNI","JULI","AGUSTUS","SEPTEMBER","OKTOBER","NOPEMBER","DESEMBER");
  return $arraymonth[$month].", $year";
}

class PDFReport extends PDF {

    function Header() {
        global $title,$perush,$page,$DEFINE_FONT;
        if ($DEFINE_FONT==0) {
            $this->AddFont('Arial','','arialn.php');
            $DEFINE_FONT=1;
        }

        list($Day,$Month,$Year,$NMonth) = GetFromDate();
        list($LMonth,$LYear,$LNMonth) = GetLastPeriod ($Month, $Year);

        $this->SetFont('Arial','',10);
        $this->Cell(180,5,$perush,0,1,'C',0);
        $this->Cell(180,5,$title,0,1,'C',0);
        $this->Cell(180,5,"PERIOD: ".BuildPeriod (GetParam("y",""), GetParam("m","")),0,1,'C',0);
        $this->Cell(180,5,$page." ".$this->PageNo(),0,1,'R',0);
        $this->Line(10,30,190,30);
        $this->Ln();
        $this->Ln();
        //Header

        //Column widths
        $w=array(80,25,25,25,25);
        $this->SetFont('Arial','',8);
        $this->Cell($w[0],10,"TR. ACCOUNT","LRBT",0,'C',0);
        $this->Cell($w[1],5,"BALANCE","LRT",0,'C',0);
        $this->Cell($w[2]+$w[3],5,strtoupper($NMonth),"LRT",0,'C',0);
//      $this->Cell($w[3],5,"","TR",0,'C',0);
        $this->Cell($w[4],5,"BALANCE","LRT",0,'C',0);
        $this->Ln();

        $this->Cell($w[0],5,"","LR",0,'C',0);
        $this->Cell($w[1],5,strtoupper($LNMonth),"LRB",0,'C',0);
        $this->Cell($w[2],5,"DEBET",1,0,'C',0);
        $this->Cell($w[3],5,"CREDIT",1,0,'C',0);
        $this->Cell($w[4],5,strtoupper($NMonth),"LRB",0,'C',0);
        $this->Ln();

    }

    function GridTable($MyResult) {
        //Column widths
        $w=array(80,25,25,25,25);

        //Data
        foreach($MyResult as $row)
        {
        $totaldebet = $totaldebet + $row["debet"];
        $totalcredit = $totalcredit + $row["credit"];
                $totallastbl = $totallastbl + $row["lastbl"];


            $this->Cell($w[0],7,$row["skada"].' - '.$row["namacc"],'LR');
            $this->Cell($w[1],7,FormatNumber($row["lastbl"]),'LR',0,'R');
        $this->Cell($w[2],7,FormatNumber($row["debet"]),'LR',0,'R');
            $this->Cell($w[3],7,FormatNumber($row["credit"]),'LR',0,'R');
        $this->Cell($w[4],7,FormatNumber($row["lastbl"]+$row["debet"]-$row["credit"]),'LR',0,'R');
            $this->Ln();
        }

        $this->Cell($w[0],7,"TOTAL:",1,0,'R',0);
        $this->Cell($w[1],7,FormatNumber(GetLastBl()),1,0,'R',0);
        $this->Cell($w[2],7,FormatNumber($totaldebet),1,0,'R',0);
        $this->Cell($w[3],7,FormatNumber($totalcredit),1,0,'R',0);
            $this->Cell($w[4],7,FormatNumber($totaldebet-$totalcredit),1,0,'R',0);
        $this->Ln();

        //Closure line
    }

    function Footer() {
        //Closure line
        $this->Cell(180,0,'','T');
    }

}


?>
